package tec.stan.ling.core.notifier;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;

import tec.stan.ling.core.notifier.impl.NotificationImpl;

public class NList<E> implements List<E> {

	private List<E> list;

	private String featureID;

	private Notifier notifier;

	public NList(List<E> list, String featureID, Notifier notifier) {
		this.list = list;
		this.featureID = featureID;
		this.notifier = notifier;
	}

	@Override
	public boolean add(E e) {
		boolean result = list.add(e);
		if (notifier.isDeliver()) {
			notifier.notify(new NotificationImpl(notifier, Notification.ADD,
					featureID, null, e));
		}
		return result;
	}

	@Override
	public void add(int index, E element) {
		list.add(index, element);
		if (notifier.isDeliver()) {
			notifier.notify(new NotificationImpl(notifier, Notification.ADD,
					featureID, null, element));
		}
	}

	@Override
	public boolean addAll(Collection<? extends E> c) {
		boolean result = list.addAll(c);
		if (notifier.isDeliver()) {
			notifier.notify(new NotificationImpl(notifier,
					Notification.ADD_MANY, featureID, null, c));
		}
		return result;
	}

	@Override
	public boolean addAll(int index, Collection<? extends E> c) {
		boolean result = list.addAll(index, c);
		if (notifier.isDeliver()) {
			notifier.notify(new NotificationImpl(notifier,
					Notification.ADD_MANY, featureID, null, c));
		}
		return result;
	}

	@Override
	public void clear() {
		ArrayList<E> oldList = new ArrayList<E>(list);
		list.clear();
		if (notifier.isDeliver()) {
			notifier.notify(new NotificationImpl(notifier,
					Notification.REMOVE_MANY, featureID, oldList, null));
		}
	}

	@Override
	public boolean contains(Object o) {
		return list.contains(o);
	}

	@Override
	public boolean containsAll(Collection<?> c) {
		return list.containsAll(c);
	}

	@Override
	public E get(int index) {
		return list.get(index);
	}

	@Override
	public int indexOf(Object o) {
		return list.indexOf(o);
	}

	@Override
	public boolean isEmpty() {
		return list.isEmpty();
	}

	@Override
	public Iterator<E> iterator() {
		return list.iterator();
	}

	@Override
	public int lastIndexOf(Object o) {
		return list.lastIndexOf(o);
	}

	@Override
	public ListIterator<E> listIterator() {
		return list.listIterator();
	}

	@Override
	public ListIterator<E> listIterator(int index) {
		return list.listIterator(index);
	}

	@Override
	public boolean remove(Object o) {
		boolean result = list.remove(o);
		if (notifier.isDeliver()) {
			notifier.notify(new NotificationImpl(notifier, Notification.REMOVE,
					featureID, o, null));
		}
		return result;
	}

	@Override
	public E remove(int index) {
		E result = list.remove(index);
		if (notifier.isDeliver()) {
			notifier.notify(new NotificationImpl(notifier, Notification.REMOVE,
					featureID, result, null));
		}
		return result;
	}

	@Override
	public boolean removeAll(Collection<?> c) {
		boolean result = list.removeAll(c);
		if (notifier.isDeliver()) {
			notifier.notify(new NotificationImpl(notifier,
					Notification.REMOVE_MANY, featureID, c, null));
		}
		return result;
	}

	@Override
	public boolean retainAll(Collection<?> c) {
		boolean result = list.retainAll(c);
		if (notifier.isDeliver()) {
			notifier.notify(new NotificationImpl(notifier,
					Notification.REMOVE_MANY, featureID, c, list));
		}
		return result;
	}

	@Override
	public E set(int index, E element) {
		return list.set(index, element);
	}

	@Override
	public int size() {
		return list.size();
	}

	@Override
	public List<E> subList(int fromIndex, int toIndex) {
		return list.subList(fromIndex, toIndex);
	}

	@Override
	public Object[] toArray() {
		return list.toArray();
	}

	@Override
	public <T> T[] toArray(T[] a) {
		return list.toArray(a);
	}
}
